package com.yjt.fresh;

import android.content.Context;


import com.yjt.fresh.lisenter.ZFConnectionListener;
import com.yjt.fresh.utils.ZFNetUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 描述：助服网帮助类
 * 作者：gxcheng
 * 日期： 2017/3/18 0018.14:30
 */

public class ZFHelper {
    private static ZFHelper instance;
    private ExecutorService executor = null;
    private Context mContext;
    /*判断网络状态是否连接*/
    private boolean mIsNetworkAble=true;

    private ZFHelper(){

    }
    public static ZFHelper getInstance(){
        if (instance==null){
            instance=new ZFHelper();
        }
        return instance;
    }

    public void init(Context context){
        this.executor = Executors.newCachedThreadPool();
        this.mContext=context.getApplicationContext();
    }

    public void addConnectionListener(final ZFConnectionListener listener) {
        if(listener != null) {
//            List var2 = this.connectionListeners;
//            synchronized(this.connectionListeners) {
//                if(!this.connectionListeners.contains(listener)) {
//                    this.connectionListeners.add(listener);
//                }
//            }

            this.execute(new Runnable() {
                public void run() {
                    if(ZFNetUtils.hasNetwork(mContext)) {
                        listener.onConnected();
                    } else {
                        listener.onDisconnected(2);
                    }

                }
            });
        }
    }

    void execute(Runnable runnable) {
        this.executor.execute(runnable);
    }

    public boolean isIsNetworkAble() {
        return mIsNetworkAble;
    }

    public void setIsNetworkAble(boolean networkAble) {
        this.mIsNetworkAble = networkAble;
    }
}
